package com.example.yml0528;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.yml.R;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class RegistActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private EditText name;
    private EditText pw;
    private EditText repw;
    private EditText phone;
    private RadioGroup sex;
    private RadioButton nan;
    private RadioButton nv;
    private CheckBox playgame;
    private CheckBox playball;
    private CheckBox swimming;
    private CheckBox sing;
    private Button regist;
    private List<String> hobby;
    private String sexstr = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        init();
    }

    public void init() {
        name = findViewById(R.id.name);
        pw = findViewById(R.id.pw);
        repw = findViewById(R.id.repw);
        phone = findViewById(R.id.phone);
        sex = findViewById(R.id.sex);
        nan = findViewById(R.id.nan);
        nv = findViewById(R.id.nv);
        playgame = findViewById(R.id.playgame);
        playball = findViewById(R.id.playball);
        swimming = findViewById(R.id.swimming);
        sing = findViewById(R.id.sing);
        regist = findViewById(R.id.regist);
        hobby = new ArrayList<>();
        playgame.setOnCheckedChangeListener(this);
        playball.setOnCheckedChangeListener(this);
        swimming.setOnCheckedChangeListener(this);
        sing.setOnCheckedChangeListener(this);
        regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String uname = name.getText().toString();
                String upw = pw.getText().toString();
                String urepw = repw.getText().toString();
                String uphone = phone.getText().toString();
                if (TextUtils.isEmpty(uname) || TextUtils.isEmpty(upw) || TextUtils.isEmpty(urepw) || TextUtils.isEmpty(uphone)) {
                    Toast.makeText(RegistActivity.this, "输入信息不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    if (upw.length() >= 0 && uname.length() >= 5) {
                        if (upw.equals(urepw)) {
                            if (hobby.size() > 0) {
                                if (uphone.length() == 11) {
                                    User user = new User();
                                    user.setUsername(uname);
                                    user.setPw(upw);
                                    user.setPhone(uphone);
                                    user.setHobby(hobby.toString());
                                    user.setSex(sexstr);
                                    Intent intent = new Intent();
                                    intent.putExtra("myuser", user);
                                    setResult(111, intent);
                                    finish();
                                } else {
                                    Toast.makeText(RegistActivity.this, "请输入有效号码", Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                Toast.makeText(RegistActivity.this, "请选择自己的爱好", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(RegistActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(RegistActivity.this, "用户名和密码请输入五位以上", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == nan.getId()) {
                    sexstr = nan.getText().toString();
                } else if (checkedId == nv.getId()) {
                    sexstr = nv.getText().toString();
                }
            }
        });
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.playgame:
                if (isChecked) {
                    hobby.add(playgame.getText().toString());
                } else {
                    hobby.remove(playgame.getText().toString());
                }
                break;
            case R.id.playball:
                if (isChecked) {
                    hobby.add(playball.getText().toString());
                } else {
                    hobby.remove(playball.getText().toString());
                }
                break;
            case R.id.swimming:
                if (isChecked) {
                    hobby.add(swimming.getText().toString());
                } else {
                    hobby.remove(swimming.getText().toString());
                }
                break;
            case R.id.sing:
                if (isChecked) {
                    hobby.add(sing.getText().toString());
                } else {
                    hobby.remove(sing.getText().toString());
                }
                break;
        }
    }

}
